﻿@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<link href="~/Content/js/simditor/css/simditor.css" rel="stylesheet" />
<script src="~/Content/js/simditor/js/module.min.js"></script>
<script src="~/Content/js/simditor/js/hotkeys.min.js"></script>
<script src="~/Content/js/simditor/js/simditor.min.js"></script>
<script>

    var editor;
    $(function () {
        editor = new Simditor({
            textarea: $('#F_content'),
            //toolbar: ['title', 'bold', 'italic', 'underline', 'strikethrough', 'color', '|', 'ol', 'ul', 'blockquote', 'code', 'table', '|', 'link', 'image', 'hr', '|', 'indent', 'outdent']
            toolbar: ['title', 'bold', 'italic', 'underline', 'strikethrough', 'color', '|', 'ol', 'ul', 'blockquote', 'table', '|', 'link', 'image', 'hr', '|', 'indent', 'outdent']
        });
    });
    var keyValue = $.request("keyValue");
    $(function () {
        if (!!keyValue) {
            $.ajax({
                url: "/MiniSite/Strategy/GetFormJson",
                data: { keyValue: keyValue },
                dataType: "json",
                async: false,
                success: function (data) {
                    $("#form1").formSerialize(data);
                    editor.setValue(data.F_content);
                }
            });
        }
    });
    function submitForm() {
        if (!$('#form1').formValid()) {
            return false;
        }
        $.submitForm({
            url: "/MiniSite/Strategy/SubmitForm?keyValue=" + keyValue,
            param: $("#form1").formSerialize(),
            success: function () {
                $.currentWindow().$("#gridList").trigger("reloadGrid");
            }
        })
    }

</script>

<form id="form1">
    <div style="margin:20px;">
        <table class="form">
            <tr>
                <th class="formTitle">攻略标题</th>
                <td class="formValue" colspan="3">
                    <input id="F_title" name="F_title" type="text" class="form-control required" placeholder="请录入攻略标题" />
                </td>
            </tr>
            <tr>
                <th class="formTitle">攻略概要</th>
                <td class="formValue" colspan="3">
                    <textarea id="F_outline" name="F_outline" class="form-control" rows="4" placeholder="请录入攻略概要"></textarea>
                </td>
            </tr>
            <tr>
                <th class="formTitle" valign="top" style="padding-top: 5px;">
                    攻略内容
                </th>
                <td class="formValue" colspan="3">
                    <div class="mail-body" style="padding: 0px;">
                        <textarea id="F_content" name="F_content" class="form-control" rows="15" style=" max-height:400px;" placeholder="请录入攻略内容"></textarea>
                    </div>
                </td>
            </tr>
            <tr>
                <th class="formTitle">上传编辑器内的显示图片</th>
                <td class="formValue">
                    <input id="F_img" type="file" class="form-control" accept="image/png,image/jpeg,image/gif">
                </td>
                <td class="formValue">
                    <input type="button" value="上传" class=" btn btn-primary" id="F_imgBtn">
                    <input type="button" value="复制图片路劲" class="btn btn-primary" id="copy_imgurlBtn">
                </td>
                <th class="formTitle"></th>
            </tr>
            <tr>
                <th></th>
                <td class="formValue" colspan="3">
                    <input type="text" id="F_imgurl"  class="form-control" name="F_imgurl" value="" placeholder="上传成功后的图片路劲" />
                </td>
            </tr>
        </table>

    </div>
</form>

<script>
    $("#F_imgBtn").on('click', function () {
        var argsId = "F_img";
        var hrefId = "F_imgurl";
        uploadSend(argsId, hrefId);

    })
    $("#F_MainimgBtn").on('click', function () {
        var argsId = "F_Mainimg";
        var hrefId = "F_MainimgUrl";
        uploadSend(argsId, hrefId);

    })
    $("#copy_imgurlBtn").on('click', function () {
        var Url2 = document.getElementById("F_imgurl");
        Url2.select(); // 选择对象
        document.execCommand("Copy"); // 执行浏览器复制命令
        alert("已复制");
    });
    function uploadSend(argsId, hrefId) {
        var data = new FormData();
        var item = document.getElementById(argsId);


        if (item != null) {
            var file = item.files[0];
            fileSize = file.size;
            fileSize = Math.round(fileSize / 1024 * 100) / 100; //kb
            if (fileSize > 2048) {
                alert('该文件超过2mb，请上传2mb以内的图片！');
                return false;
            }
            data.append(file.name, file);
            var xhr = new XMLHttpRequest();
            xhr.onload = function () {
                data = JSON.parse(xhr.responseText);
                if (data.state == 1) {
                    console.log(data);
                    document.getElementById(hrefId).value = data.data + "-400x267";
                    alert("上传成功");
                } else {
                    alert(data.msg);
                }
            };
            xhr.onerror = function (err) {
                console.error(err);
                document.getElementById(hrefId).value = '';
            };
            xhr.open('post', '/SiteManage/Site/UploadAttachment', true);
            xhr.send(data);
        }
    }
</script>


